rm(list = ls())
require(stargazer)

data21=read.csv("D:/Documents and Work/Dean Stuff/data/columbia_experiment_december6noon_type7clean.csv", header=TRUE)
data22=read.csv("D:/Documents and Work/Dean Stuff/data/columbia_experiment_december15_type7clean.csv", header=TRUE)
data23=read.csv("D:/Documents and Work/Dean Stuff/data/columbia_experiment_january26_type7(clean).csv", header=TRUE)


dataframe21=data.frame(data21)
dataframe22=data.frame(data22)
dataframe23=data.frame(data23)

#some definitions
expframe=rbind(dataframe21,dataframe22,dataframe23)
expframe=subset(expframe,uid>800)
expframe$correct <-(expframe$state==4&(expframe$response==2|expframe$response==7|expframe$response==8|expframe$response==9))|(expframe$state==3&(expframe$response==1|expframe$response==6|expframe$response==3|expframe$response==5))
expframe$incentive <- (expframe$qid==5)*5+(expframe$qid==1)*40+(expframe$qid==6)*70+(expframe$qid==7)*95
accuracy=c(sum((expframe$incentive==5)*expframe$correct)/sum(expframe$incentive==5),sum((expframe$incentive==40)*expframe$correct)/sum(expframe$incentive==40),sum((expframe$incentive==70)*expframe$correct)/sum(expframe$incentive==70),sum((expframe$incentive==95)*expframe$correct)/sum(expframe$incentive==95))
incentivec=c(5,40,70,95)
uidlist=unique(expframe$uid)


#adjust values based on priors
#correct
avgcorrect=sum(expframe$correct)/length(expframe$correct)
avgcorrect0.5=(expframe$priorA==0.5)*(sum(expframe$correct*(expframe$priorA==0.5))/sum(expframe$priorA==0.5))
avgcorrect0.6=(expframe$priorA==0.6)*sum(expframe$correct*(expframe$priorA==0.6))/sum(expframe$priorA==0.6)
avgcorrect0.75=(expframe$priorA==0.75)*sum(expframe$correct*(expframe$priorA==0.75))/sum(expframe$priorA==0.75)
avgcorrect0.85=(expframe$priorA==0.85)*sum(expframe$correct*(expframe$priorA==0.85))/sum(expframe$priorA==0.85)
expframe$correctmod=expframe$correct+avgcorrect-avgcorrect0.5-avgcorrect0.6-avgcorrect0.75-avgcorrect0.85



uidlist=unique(expframe$uid)


N=1000
n=length(uidlist)
randomdraw=sample(1:n,n*N,replace=TRUE)

probcorrectblock2ef=vector('numeric')
probcorrectblock3ef=vector('numeric')
probcorrectblock4ef=vector('numeric')


for(i in 1:N){
bootframe=data.frame()
for(j in 1:n){
addframe=subset(expframe,uid==uidlist[randomdraw[(i-1)*n+j]])
bootframe=rbind(bootframe,addframe)
}
bootframe1=subset(bootframe,blocknum==1)
bootframe2=subset(bootframe,blocknum==2)
bootframe3=subset(bootframe,blocknum==3)
bootframe4=subset(bootframe,blocknum==4)
probcorrectblock2ef[i]=sum(bootframe2$correct)/length(bootframe2$correct)-sum(bootframe1$correct)/length(bootframe1$correct)
probcorrectblock3ef[i]=sum(bootframe3$correct)/length(bootframe3$correct)-sum(bootframe1$correct)/length(bootframe1$correct)
probcorrectblock4ef[i]=sum(bootframe4$correct)/length(bootframe4$correct)-sum(bootframe1$correct)/length(bootframe1$correct)

}
#clean
probcorrectblock2ef=na.omit(probcorrectblock2ef)
probcorrectblock3ef=na.omit(probcorrectblock3ef)
probcorrectblock4ef=na.omit(probcorrectblock4ef)

lower2=round(length(probcorrectblock2ef)*0.025,1)
lower3=round(length(probcorrectblock3ef)*0.025,1)
lower4=round(length(probcorrectblock4ef)*0.025,1)

upper2=length(probcorrectblock2ef)-lower2
upper3=length(probcorrectblock3ef)-lower3
upper4=length(probcorrectblock4ef)-lower4


sortprobcorrectblock2ef=sort(probcorrectblock2ef)
sortprobcorrectblock3ef=sort(probcorrectblock3ef)
sortprobcorrectblock4ef=sort(probcorrectblock4ef)


avgprobcorrectblock2ef=mean(probcorrectblock2ef)
avgprobcorrectblock3ef=mean(probcorrectblock3ef)
avgprobcorrectblock4ef=mean(probcorrectblock4ef)

upperprobcorrectblock2ef=sortprobcorrectblock2ef[upper2]
upperprobcorrectblock3ef=sortprobcorrectblock3ef[upper3]
upperprobcorrectblock4ef=sortprobcorrectblock4ef[upper4]


lowerprobcorrectblock2ef=sortprobcorrectblock2ef[lower2]
lowerprobcorrectblock3ef=sortprobcorrectblock3ef[lower3]
lowerprobcorrectblock4ef=sortprobcorrectblock4ef[lower4]

i=1
while((sortprobcorrectblock2ef[i]<0)&(sortprobcorrectblock2ef[length(sortprobcorrectblock2ef)+1-i]>0)){
i=i+1
}
pvalblock2=2*i/N
i=1
while((sortprobcorrectblock3ef[i]<0)&(sortprobcorrectblock3ef[length(sortprobcorrectblock3ef)+1-i]>0)){
i=i+1
}
pvalblock3=2*i/N
i=1
while((sortprobcorrectblock4ef[i]<0)&(sortprobcorrectblock4ef[length(sortprobcorrectblock4ef)+1-i]>0)){
i=i+1
}
pvalblock4=2*i/N



orderreport=data.frame(average=c(avgprobcorrectblock2ef,avgprobcorrectblock3ef,avgprobcorrectblock4ef),lowerCI=c(lowerprobcorrectblock2ef,lowerprobcorrectblock3ef,lowerprobcorrectblock4ef),upperCI=c(upperprobcorrectblock2ef,upperprobcorrectblock3ef,upperprobcorrectblock4ef), Pval=c(pvalblock2,pvalblock3,pvalblock4))
rownames(orderreport)=c("Block 2","Block 3","Block 4")

#Tabl eA3.1**
stargazer(orderreport,summary=FALSE)
#*************



